package com.bw.dao;

import com.bw.pojo.User;
import com.bw.util.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/***************************
 FileAction:

 CreateTime:2022-05-10 8:30
 Author:Aaking-aline
 ****************************/
public class UserDao {

    public boolean selectUserByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select * from user where username = ? and password = ?";

        Connection conn = JDBC.getConnection();

        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setObject(1,username);
        ps.setObject(2,password);

        ResultSet rs = ps.executeQuery();

        if(rs.next()){
            return true;
        }

        return false;
    }

    public void insertUser(User user) throws SQLException {
        String sql = "insert into user values(null,?,?,?,?,?,?,?,?,?)";
        Connection conn = JDBC.getConnection();

        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setObject(1,user.getUsername());
        ps.setObject(2,user.getPassword());
        ps.setObject(3,user.getReal_name());
        ps.setObject(4,user.getNike_name());
        ps.setObject(5,user.getAge());
        ps.setObject(6,user.getGender());
        ps.setObject(7,user.getBirthday());

        String str = "";
        String[] hobby = user.getHobby();
        for (String s : hobby) {
            str += s;
            str += ",";
        } // 篮球,羽毛球,足球,

        ps.setObject(8,str.substring(0,str.length()-1));

        ps.setObject(9,user.getPhone_num());

        ps.executeUpdate();
    }
}
